package cn.xh.system.controller;

import cn.xh.basic.util.AjaxResult;
import cn.xh.basic.util.PageList;

import cn.xh.system.domain.Systemdictionarydetail;
import cn.xh.system.domain.Systemdictionarytype;
import cn.xh.system.query.SystemdictionarydetailQuery;
import cn.xh.system.service.ISystemdictionarydetailService;
import cn.xh.system.service.ISystemdictionarytypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 系统管理接口
 * 写RestFull风格的接口
 * 写接口的三要素
 * 1.访问路径是什么
 * 2.参数是什么
 * 3.返回值是什么
 * <p>
 * 什么是RestFull风格的接口
 * 1.使用URL来定位资源
 * 2.使用http的动词来描述操作
 */
@RestController
@RequestMapping("/sysdetail")
@Api(tags = "系统接口", description = "系统接口详细描述")
public class SystemdictionarydetailController {
    @Autowired
    private ISystemdictionarydetailService systemdictionarydetailService;
    @Autowired
    private ISystemdictionarytypeService systemdictionarytypeService;


    //新增和修改  put
    @PutMapping
    @ApiOperation(value = "系统添加或修改", notes = "如果有id就是修改否则就是添加")
    public AjaxResult addOrUpdate(@RequestBody Systemdictionarydetail systemdictionarydetail) {
        try {
            if (systemdictionarydetail.getId() != null) {
                systemdictionarydetailService.update(systemdictionarydetail);
            } else {
                systemdictionarydetailService.add(systemdictionarydetail);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setMessage(e.getMessage());
        }
    }

    //删除  delete

    /**
     * PathVariable  从URL中获取占位符的值
     *
     * @param id
     */
    @DeleteMapping("/{id}")   //  /dept/1
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            systemdictionarydetailService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setMessage(e.getMessage());
        }
    }

    //查询一条  get
    @GetMapping("/{id}")
    public Systemdictionarydetail get(@PathVariable("id") Long id) {
        return systemdictionarydetailService.queryById(id);
    }

    //查询所有  get
    @GetMapping("/list")
    public List<Systemdictionarydetail> getAll() {
        return systemdictionarydetailService.queryAll();
    }

    //分页+高级查询 post
    @PostMapping
    public PageList<Systemdictionarydetail> getPage(@RequestBody SystemdictionarydetailQuery query) {
        return systemdictionarydetailService.queryPage(query);
    }

    @GetMapping("/detail")
    public List<Systemdictionarytype> getSystemdictionarytypes() {
        return systemdictionarytypeService.queryAll();
    }

}
